package com.ag.lzw.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import keeper.control.Controller;
import keeper.event.Event;
import keeper.event.EventListener;
import keeper.util.NameRegister;
import keeper.util.RegistException;

public class NotifyObserver extends HttpServlet {

	private static final long serialVersionUID = 6999818298972394938L;

	public NotifyObserver() {
		super();
	}

	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println("\n\t*** doGet... \t***");
		String eventName = request.getParameter("event");
		NameRegister reg = NameRegister.getInstance();
		Event event = null;
		try {
			event = reg.getEvent(eventName);
			if (event == null) {
				event = new Event(eventName, this);
				reg.registeEvent(eventName, event);
			}
			if (event.getServlet() == null) {
				event.setServlet(this);
			}

		} catch (RegistException e1) {
			e1.printStackTrace();
		}
		synchronized (this) {
			while (!event.isProcess()) {
				try {
					wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		EventListener listener = event.getListener();
		if (listener != null) {
			listener.process(request, response);
		}
		
/*		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println("\n\t**** doPost... \t ****");
		String ftid = request.getParameter("ftid"); //获取上级任务ID参数
		String tid = request.getParameter("tid");  //获取任务ID参数
		if(ftid!=null  && tid != null){
			Controller.action(new SendMsg(tid), ftid); //通知所有上级任务的宿主本任务的ID
		} else {
			PrintWriter out = response.getWriter();
			out.print("error");
		}
		
/*		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
	}


	public void init() throws ServletException {

	}

}
